package com.kh.statistics.video.core.pool;

import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @BelongsProject: statistics
 * @BelongsPackage: com.kh.statistics.video.core.pool
 * @Author: Hong.Kong
 * @CreateTime: 2024-05-24  10:54
 * @Description: TODO
 * @Version: 1.0
 */
@Component
public class ScheduledThreadPoolTaskManager implements TaskManager {
    // 创建ScheduledThreadPool，参数为线程池的大小
    ScheduledThreadPoolExecutor scheduledThreadPool = new ScheduledThreadPoolExecutor(2);

    @Override
    public void addTask(RunnableTask runnableTask, long initialDelay, long delay, TimeUnit unit) {
        scheduledThreadPool.scheduleWithFixedDelay(runnableTask, initialDelay, delay, unit);
    }

    @Override
    public void close() throws IOException {
        System.out.println("最后执行了关闭定时线程池");
        scheduledThreadPool.shutdown();
    }
}
